package com.cn.system.service.impl;

import com.cn.system.dao.RoleDao;
import com.cn.system.dao.UserDao;
import com.cn.system.domain.RoleDO;
import com.cn.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleMapper;
    @Autowired
    UserDao userMapper;

    @Override
    public List<RoleDO> list() {
        List<RoleDO> roles = roleMapper.list();
        return roles;
    }

    @Override
    public Set<String> listUserRoleIds(String userId) {
        List<String> rolesIds = roleMapper.rolesIdsByUserId(userId);
        return new HashSet<>(rolesIds);
    }

    @Override
    public List<RoleDO> listUserRoles(String userId) {
        List<RoleDO> roles = roleMapper.list();
        if (!"".equalsIgnoreCase(userId) && userId != null) {
            List<String> rolesIds = roleMapper.rolesIdsByUserId(userId);
            for (RoleDO roleDO : roles) {
                roleDO.setRoleSign("false");
                for (String roleId : rolesIds) {
                    if (Objects.equals(roleDO.getRoleId(), roleId)) {
                        roleDO.setRoleSign("true");
                        break;
                    }
                }
            }
        }
        return roles;
    }

    @Transactional
    @Override
    public int save(RoleDO role) {
        int count = roleMapper.save(role);
        List<String> menuIds = role.getMenuIds();
        String roleId = role.getRoleId();
//        List<RoleMenuDO> rms = new ArrayList<>();
//        for (String menuId : menuIds) {
//            RoleMenuDO rmDo = new RoleMenuDO();
//            rmDo.setRoleId(roleId);
//            rmDo.setMenuId(menuId);
//            rms.add(rmDo);
//        }
//        roleMenuMapper.removeByRoleId(roleId);
//        if (rms.size() > 0) {
//            roleMenuMapper.batchSave(rms);
//        }
        return count;
    }

    @Transactional
    @Override
    public int remove(String id) {
        int count = roleMapper.remove(id);
//        userRoleMapper.removeByRoleId(id);
//        roleMenuMapper.removeByRoleId(id);
        return count;
    }


    @Override
    public RoleDO get(String id) {
        RoleDO roleDO = roleMapper.get(id);
        return roleDO;
    }

    @Override
    public int update(RoleDO role) {
        int r = roleMapper.update(role);
        List<String> menuIds = role.getMenuIds();
        String roleId = role.getRoleId();
//        roleMenuMapper.removeByRoleId(roleId);
//        List<RoleMenuDO> rms = new ArrayList<>();
//        for (String menuId : menuIds) {
//            RoleMenuDO rmDo = new RoleMenuDO();
//            rmDo.setRoleId(roleId);
//            rmDo.setMenuId(menuId);
//            rms.add(rmDo);
//        }
//        if (rms.size() > 0) {
//            roleMenuMapper.batchSave(rms);
//        }
        return r;
    }

    @Override
    public int batchremove(String[] ids) {
        int r = roleMapper.batchRemove(ids);
        return r;
    }


}
